package pers.cz.exception;

import org.pentaho.di.core.exception.KettleDatabaseException;
import pers.cz.pub.RespBean;
import org.springframework.security.core.context.SecurityContextHolder;
import pers.cz.message.MessageType;
import pers.cz.message.MessageUtil;
import org.pentaho.di.core.exception.KettleException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import pers.cz.websocket.MessagePushSocketUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;

/**
 * 全局异常处理类
 * @program: PostGirl-panent
 * @description: GlobalExceptionHandler
 * @author: Cheng Zhi
 * @create: 2021-03-11 12:56
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(KettleException.class)
    public RespBean KettleException(KettleException e) {
        // 这里通知前台
        String content = "notify|【数据同步失败！】" + e.getMessage();
        // 将结果记录到数据库。默认都是未读的
        if (content.length() > 999) {
            MessageUtil.insertNotifyMessage(content.substring(0,998), MessageType.getName("notify"), "未读", null);
        } else {
            MessageUtil.insertNotifyMessage(content, MessageType.getName("notify"), "未读", null);
        }
        // 发送实时消息
        MessagePushSocketUtils.sendInfo(content,Integer.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()));
        return RespBean.error("数据同步异常");
    }

    @ExceptionHandler(KettleDatabaseException.class)
    public RespBean KettleDatabaseException(KettleDatabaseException e) {
        // 这里通知前台
        String content = "notify|【数据同步失败！】" + e.getMessage();
        // 将结果记录到数据库。默认都是未读的
        if (content.length() > 999) {
            MessageUtil.insertNotifyMessage(content.substring(0,998), MessageType.getName("notify"), "未读", null);
        } else {
            MessageUtil.insertNotifyMessage(content, MessageType.getName("notify"), "未读", null);
        }
        // 发送实时消息
        MessagePushSocketUtils.sendInfo(content,Integer.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()));
        return RespBean.error("数据同步异常");
    }

    @ExceptionHandler(URISyntaxException.class)
    public RespBean uRISyntaxException(URISyntaxException e) {
        return RespBean.error("请求异常，请检查请求类型是否正确");
    }

    @ExceptionHandler(IOException.class)
    public RespBean iOException(IOException e) {

        return RespBean.error("文件操作异常");
    }

    //@ExceptionHandler(NullPointerException.class)
    public RespBean nullPointerException(NullPointerException e) {

        return RespBean.error("空指针异常喽。。。");
    }
    /**
     * 处理并返回业务规则校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public RespBean businessException(BusinessException e) {
        String messageCode = e.getMessageCode();
        String errorMessage = ExceptionUtil.getBaseExceptionText(e);
        return RespBean.error("业务规则校验不通过。" + " 业务错误编码：" + messageCode + " 错误消息:" + errorMessage );
    }

    /**
     * 处理并返回基本异常
     * @param e
     * @return
     */
    @ExceptionHandler(GeneralException.class)
    public RespBean dataAccessException(GeneralException e) {

        return RespBean.error("发生了一点儿小意外", e.getCause());
    }



}
